from flask import Flask
from sqlalchemy import Column, DateTime, Integer, String, desc
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy
import pymysql
from .config import *


app = Flask(__name__)
pymysql.install_as_MySQLdb()

class Config(object):
    """配置参数"""
    # 设置连接数据库的URL
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s' % (DB_USER,DB_PWD,DB_HOST,DB_PORT,DB_NAME)
    # 设置sqlalchemy自动更跟踪数据库
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    # 查询时会显示原始SQL语句
    app.config['SQLALCHEMY_ECHO'] = False
    # 禁止自动提交数据处理
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = False

# 读取配置
app.config.from_object(Config)

# 创建数据库sqlalchemy工具对象
db = SQLAlchemy(app)


class Project(db.Model):

    __tablename__ = 'project'

    project = db.Column(db.Integer, primary_key=True,unique=True)  # id 整型，主键，自增，唯一
    iap = db.Column(db.Text)
    basic = db.Column(db.Text)
    classify = db.Column(db.Text)
    view = db.Column(db.Text)
    purchase_limit = db.Column(db.Text)
    bonus = db.Column(db.Text)


class SchemaConfigData(db.Model):
    # __tablename__ = "sub_game_config_data_copy1"
    __tablename__ = "schema_config_data"
    id = db.Column(db.Integer, primary_key=True, unique=True)  # id 整型，主键，自增，唯一
    file_name = db.Column(db.String)
    data = db.Column(db.Text)
    update_time = db.Column(db.Integer)
    identify = db.Column(db.String)
    remark = db.Column(db.String)
    author = db.Column(db.String)

    def to_dict(self):
        pass


class SysLog(db.Model):
    __tablename__ = 'sys_log'
    id = db.Column(db.Integer, primary_key=True, unique=True)
    module_id = db.Column(db.Integer)
    export_id = db.Column(db.Integer)
    inner_id = db.Column(db.Integer)
    log_desc = db.Column(db.Text)
    log_info = db.Column(db.Text)
    update_time = db.Column(db.Integer)
    source = db.Column(db.Text)
    user = db.Column(db.Text)
    user_id = db.Column(db.Integer)
    part = db.Column(db.Text)
    ip_address = db.Column(db.Text)
    additional_info = db.Column(db.Text)
    status = db.Column(db.Text)



    def __init__(self, dictionary):
        for key, value in dictionary.items():
            setattr(self, key, value)

        # 检查字典中不存在的字段，并将其赋值为空
        for key in set(dictionary.keys()).difference(set(vars(self).keys())):
            setattr(self, key, None)

        db.Model.__init__(self)

